%%
%%
%%
%%
clear;
clf;
% Funcion Blending
%
%
%
% Valores Parametricos
u= 0 : 0.05 : 1;
for i=1:21
    U(i,1)=u(i)^3;
    U(i,2)=u(i)^2;
    U(i,3)=u(i);
    U(i,4)=1;
end
w= 0 : 0.05 : 1;
for i=1:21
    W(i,1)=w(i)^3;
    W(i,2)=w(i)^2;
    W(i,3)=w(i);
    W(i,4)=1;
end
% Matrix de transformacion de bezier cubica
M=  [  -1  3 -3 1;
    3 -6  3 0;
    -3  3  0 0;
    1  0  0 0;];
% Control de Vertices del patch1
% P00 p10 p20 p30 p01 p11 p21 p31 p02 p12 p22 p32 p03 p13 p23 p33

patch1 = [-5, -2, 2, 5, -4, -2.5, 2.5, 4, -8.5, -4.5, 4.5, 8.5, -7, -3, 3, 7;
    1, 3, 3, 1, 1, 2, 2, 1, -1, 0, 0, -1, 2, 4, 4, 2;
    -6, -6, -6, -6, -3, -3, -3, -3, 1, 3, 3, 1, 5, 6.5, 6.5, 5;
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];

% Control de Vertices del patch1
CV_X=[ patch1(1,1) patch1(1,5) patch1(1,9) patch1(1,13);
    patch1(1,2) patch1(1,6) patch1(1,10) patch1(1,14);
    patch1(1,3) patch1(1,7) patch1(1,11) patch1(1,15);
    patch1(1,4) patch1(1,8) patch1(1,12) patch1(1,16);];

CV_Y=[ patch1(2,1) patch1(2,5) patch1(2,9) patch1(2,13);
    patch1(2,2) patch1(2,6) patch1(2,10) patch1(2,14);
    patch1(2,3) patch1(2,7) patch1(2,11) patch1(2,15);
    patch1(2,4) patch1(2,8) patch1(2,12) patch1(2,16);];

CV_Z=[ patch1(3,1) patch1(3,5) patch1(3,9) patch1(3,13);
    patch1(3,2) patch1(3,6) patch1(3,10) patch1(3,14);
    patch1(3,3) patch1(3,7) patch1(3,11) patch1(3,15);
    patch1(3,4) patch1(3,8) patch1(3,12) patch1(3,16);];

% Superficie de Bezier
for i=1:21
    for j=1:21
        P_X(i,j)=U(i,:)*M*CV_X*M'*W(j,:)';
        P_Y(i,j)=U(i,:)*M*CV_Y*M'*W(j,:)';
        P_Z(i,j)=U(i,:)*M*CV_Z*M'*W(j,:)';
    end
end
% Rango de la figura
MIN_X=-8;
MAX_X=8;
MIN_Y=-8;
MAX_Y=8;
MIN_Z=-8;
MAX_Z=8;

% Dibujar la superficie de bezier
figure(1);
axis ([MIN_X, MAX_X, MIN_Y, MAX_Y, MIN_Z, MAX_Z]);
title('Superficie de Bezier');
xlabel('X');
ylabel('Y');
Zlabel('Z');
hold on;
for i=1:21
    plot3 (P_X(:,i), P_Y(:,i), P_Z(:,i), 'k');
    plot3 (P_X(i,:), P_Y(i,:), P_Z(i,:), 'k');
end
for i=1:4
    plot3 (CV_X(:,i), CV_Y(:,i), CV_Z(:,i), ':');
    plot3 (CV_X(i,:), CV_Y(i,:), CV_Z(i,:), ':');
end
rotate3d
hold off;

